﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace HHMMS.FileServer
{
	public static class XmlNodeHelper
	{
		public static bool? GetBooleanNullValue(XmlNode fromNode, string xpath, bool? defaultValue)
		{
			object obj2 = GetNodeValue<bool?>(fromNode, xpath, defaultValue);
			if (obj2 == null)
			{
				return null;
			}
			return new bool?(Convert.ToBoolean(obj2));
		}

		public static bool GetBooleanValue(XmlNode fromNode, string xpath, bool defaultValue)
		{
			return Convert.ToBoolean(GetNodeValue<bool>(fromNode, xpath, defaultValue));
		}

		public static int GetIntValue(XmlNode fromNode, string xpath, int defaultValue)
		{
			return Convert.ToInt32(GetNodeValue<int>(fromNode, xpath, defaultValue));
		}

		public static long GetLongValue(XmlNode fromNode, string xpath, long defaultValue)
		{
			return Convert.ToInt64(GetNodeValue<long>(fromNode, xpath, defaultValue));
		}

		private static object GetNodeValue<T>(XmlNode fromNode, string xpath, T defaultValue)
		{
			XmlNode node = fromNode.SelectSingleNode(xpath);
			if (node == null)
			{
				return defaultValue;
			}
			string str = (node.NodeType == XmlNodeType.Element) ? node.InnerText : node.Value;
			if (string.IsNullOrEmpty(str))
			{
				return defaultValue;
			}
			return str;
		}

		public static string GetStringValue(XmlNode fromNode, string xpath, string defaultValue)
		{
			return Convert.ToString(GetNodeValue<string>(fromNode, xpath, defaultValue));
		}
	}
}
